from sale.apps.member.models import UserInfo, OrderInfo
from sale.apps.member.models import VerifyCode
from sale.apps.member.models import Phone
from sale.apps.member.models import PackageInfo
from sale.utils import db


def get_user_info_by_name(name):
    user = UserInfo.query.filter(UserInfo.name == name).first()
    return user


def get_user_info_by_phone(phone):
    user = UserInfo.query.filter(UserInfo.phone == phone).first()
    return user


def add_user_info(name_l, phone_l, password_l):
    user = UserInfo(name=name_l, phone=phone_l, password=password_l)
    db.session.add(user)
    db.session.flush()
    db.session.commit()
    return user


def add_verify_code(phone, code, time):
    verify_code = VerifyCode(phone=phone, verify_code=code, create_time=time)
    db.session.add(verify_code)
    db.session.commit()
    return verify_code


def query_verify_code_by_pt(phone, time_):
    record = VerifyCode.query.filter(
        VerifyCode.phone == phone,
        VerifyCode.create_time >= time_
    ).first()
    return record


def count_verify_code_by_pt(phone, time_):
    count = VerifyCode.query.filter(
        VerifyCode.phone == phone,
        VerifyCode.create_time >= time_
    ).count()
    return count


def query_unused_verify_code(phone, verify_code):
    record = VerifyCode.query.filter(
        VerifyCode.phone == phone,
        VerifyCode.verify_code == verify_code,
        VerifyCode.status == 0
    ).order_by(VerifyCode.create_time.desc()).first()
    return record


#  修改验证码的状态
def update_verify_code(phone, verify_code):
    record = VerifyCode.query.filter(
        VerifyCode.phone == phone,
        VerifyCode.verify_code == verify_code
    ).first()
    record.status = 1
    db.session.commit()
    return record


def add_acquire_phone(phone):
    acquire_phone = Phone(phone=phone)
    db.session.add(acquire_phone)
    db.session.commit()
    return acquire_phone


def add_package_info(original_price, discount_price):
    price_info = PackageInfo(original_price=original_price, discount_price=discount_price)
    db.session.add(price_info)
    db.session.commit()
    return price_info


def select_package():
    package_info = PackageInfo.query.with_entities(
        PackageInfo.id,
        PackageInfo.original_price,
        PackageInfo.discount_price
    ).all()
    return package_info


def select_package_by_id(package_id):
    ret = PackageInfo.query.filter(PackageInfo.id == package_id).first()
    return ret


def add_create_order(user_id, user_phone, package_id, face_value, pay_amount, order_no, deposit_phone, goods_desc):
    order_info = OrderInfo(
        user_id=user_id,
        user_phone=user_phone,
        package_id=package_id,
        face_value=face_value,
        pay_amount=pay_amount,
        order_no=order_no,
        pay_no=deposit_phone,
        goods_desc=goods_desc
    )
    db.session.add(order_info)
    db.session.flush()
    return order_info


def select_my_orders(user_phone):
    user_order = OrderInfo.query.filter(OrderInfo.user_phone == user_phone).all()
    return user_order


def select_order_info(order_no):
    order_info = OrderInfo.query.filter(OrderInfo.order_no == order_no).first()
    return order_info
